package autoFile.core.strategy;

import autoFile.core.strategy.interfaces.CreateFileStrategyInterface;
import com.google.common.base.CaseFormat;

import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: liuliwei
 * Date: 2017-08-16
 * Time: 下午4:29
 * Description: ${description}
 */
public class CreateGongRpcConstantsStrategy extends AbstractStrategy implements CreateFileStrategyInterface {

    String fileName = "/Users/liuliwei/autoFile/GongRpcConstants.java";

//    private FileWriter gongRpcConstantsWriter = new FileWriter("/Users/liuliwei/autoFile/GongRpcConstants.java", true);
    private FileWriter gongRpcConstantsWriter = new FileWriter(fileName, true);


    public CreateGongRpcConstantsStrategy() throws IOException {
    }

    @Override
    public void createFile(Class clazz) throws IOException {
        List<Method> publicMethod = getPublicMethod(clazz);
        System.out.println("生成"+fileName);
        for (Method method : publicMethod) {
            String className = clazz.getSimpleName(); //类名称
            if (className.contains("Service")) {
                className = className.replaceFirst("Service", "");
            }
            System.out.print(createGongRpcConstants(className, method, gongRpcConstantsWriter));

        }
        System.out.println();
        System.out.println();
        gongRpcConstantsWriter.close();
    }

    /**
     * @param simpleName
     * @param method
     * @param fileWriter
     * @return
     * @throws IOException
     */
    private static String createGongRpcConstants(String simpleName, Method method, FileWriter fileWriter) throws IOException {
        simpleName = CaseFormat.UPPER_CAMEL.converterTo(CaseFormat.LOWER_CAMEL).convert(simpleName);
        String methodName = CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.UPPER_CAMEL).convert(method.getName());
        String name = simpleName + methodName;
        String pre = "public static final String ";
        String content = "REQ_" + CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.UPPER_UNDERSCORE).convert(name);
        String suf = " = \"u." + name + "\";";
        String returnString = "GongRpcConstants" + content;
        String gongRpcConstant = pre + content + suf + "\n";
        fileWriter.write(gongRpcConstant);
        return gongRpcConstant;
    }
}
